<?php


namespace api\channel\JdUnion;

use api\channel\ExtendChannleBase;
use app\model\BaseSupplier;
use Dml\EasyTBK\Factory;
use think\Exception;
/**
 * 京东联盟基础类
 */
class Base extends ExtendChannleBase
{

    /**
     * 请求地址
     * @var string $bestUrl
     */
    protected $bestUrl;

    /**
     * 密匙apikey
     * @var string $apiKey
     */
    protected $apiKey;

    /**
     * 密匙appkey
     * @var string $secretKey
     */
    protected $secretKey;

    /**
     * 渠道商信息
     * @var object $supplier
     */
    protected $supplier;

    /**
     * 接口返回信息
     * @var object $codeMsg
     */
    protected $codeMsg;

    protected $jd;

    /**
     *
     * 开发文档 https://union.jd.com/searchResultDetail?articleId=108188
     * @throws Exception
     */
    public function __construct()
    {
        //供应商参数
        $info = BaseSupplier::where('code', BaseSupplier::CODE_JINGDONGUNION)
            ->where('serv_type',PRODUCT_MOBILE_CPS_SERV)
            ->cache(BaseSupplier::CODE_JINGDONGUNION.PRODUCT_MOBILE_CPS_SERV,'3600')
            ->find();
        if (!$info) {
            throw new Exception('不存在京东联盟配置信息');
        }
        if (empty($info->api_config)) {
            throw new Exception('缺少配置信息');
        }
        $this->supplierInfo = $info;

        $this->supplier = $info;

        $config = is_string($info->api_config) ? json_decode($info->api_config, true) : $info->api_config;

        $this->apiKey = trim($config['apiKey']);

        $this->secretKey = trim($config['secretKey']);

        $this->bestUrl = trim($config['bestUrl']);

        $this->jd = Factory::jingdong([
            'app_key'    =>  $this->apiKey,
            'app_secret' =>  $this->secretKey,
            'format'     =>  'json',
        ]);
    }

    /**
     * Notes:获取apikey
     * User: ymzhang
     * Date: 2022/07/29
     * Time: 11:50
     * @return string
     */
    public function getApiKey()
    {
        return $this->apiKey;
    }

    /**
     * Notes:获取secretkey
     * User: ymzhang
     * Date: 2022/07/29
     * Time: 11:50
     * @return string
     */
    public function getSecretkey()
    {
        return $this->secretKey;
    }

    /**
     * Notes:获取密匙apiKey
     * User: ymzhang
     * Date: 2022/06/28
     * Time: 15:45
     * @return string
     */
    public function getSupplier()
    {
        return $this->supplier;
    }


    /**
     * Notes:获取接口地址
     * User: ymzhang
     * Date: 2022/07/29
     * Time: 11:50
     * @return string
     */
    public function getBaseUrl()
    {
        return $this->bestUrl;
    }

    /**
     * Notes:返回供应商id
     * User: ymzhang
     * Date: 2022/07/29
     * Time: 11:50
     * @return string
     */
    public function getSupplierId()
    {
        return $this->supplier['id'];
    }

    /**
     * Notes:获取提示消息
     * User: ymzhang
     * Date: 2022/06/29
     * Time: 13:50
     * @param string $code
     * @return string
     */
    public function getMsg(string $code)
    {
        return $this->codeMsg[$code]??'';
    }
}